package pricingTag;

/*
 * what function should be implemented in store object?
 * 1) negotiation about one product
 * 2)  
 * 
 */
import java.util.*;

public class Global {
	static int store_id = 0;
	List<Store> stores;
	List<Deal> deals;
	List<ProductRecord> product_records;
	List<Reply> replys;

	// List<String> product_names;

	public Global() {
		stores = new ArrayList<Store>();
		deals = new ArrayList<Deal>();
		product_records = new ArrayList<ProductRecord>();
		replys = new ArrayList<Reply>();
		// this.product_names = new ArrayList<String>();

	}

	/*
	 * in the version 1, each store is forced to make a deal. this deal is sent
	 * to other store, each store will consider this deal and accept or refuse
	 * the given deal. return the accepted deal or null otherwise.
	 */
	void negotiate(String product_name) {
		// each store is asked to generate a deal about this product
		for (Store store : this.stores) {
			Deal deal = store.generate_deal(product_name);
			if (deal != null) {
				deals.add(deal);
			}
		}
		// for (Store store : this.stores) {
		// for (Deal deal : deals) {
		// Reply reply = store.generate_reply(deal);
		// if (reply != null) {
		// this.replys.add(reply);
		// }
		// }
		// }
		// return null;
	}

	Reply send_deal_to(Store to_store, Deal deal) {
		Reply reply = to_store.generate_reply(deal);
		return reply;

	}

	Deal send_reply_to(Store to_store, Reply reply) {
		Deal deal = to_store.consider_reply(reply);
		return deal;
	}

	void broadcast_deal(Deal deal) {
		for (Store store : stores) {
			if (!store.equals(deal.from_store)) {
				store.generate_reply(deal);
			}
		}
	}

	// show all deals in global currently
	void show_deals() {
		if (deals.size() != 0) {
			for (Deal deal : deals) {
				deal.show();
			}
		} else {
			System.out.println("There is not deals right now");
		}
	}

	void show_replys() {
		if (replys.size() != 0) {
			for (Reply reply : replys) {
				reply.show();
			}
		} else {
			System.out.println("There is not replys right now");
		}
	}

	void show_stores() {

		System.out.format("\nCurrently, this is %d stores:\n",
				this.stores.size());
		for (Store store : this.stores) {
			System.out.println(store);
			store.show_products();
			System.out.println();

		}
	}

	void show_product_records() {
		for (ProductRecord pr : this.product_records) {
			pr.show();
		}
	}

	ProductRecord find_product_record(String name) {
		for (ProductRecord pr : this.product_records) {
			if (pr.name.equals(name)) {
				return pr;
			}
		}
		return null;
	}

}
